﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DoAnPhatTrienPhanMem.Models;
using System.Data;
using System.Globalization;
using DoAnPhatTrienPhanMem.Constants;

namespace DoAnPhatTrienPhanMem.Controllers
{
    [ValidateInput(false)]
    [HandleError]
    public class BaseController : Controller
    {
        protected LinqHelper<DAPTPMDataContext> helper = new LinqHelper<DAPTPMDataContext>();

        protected JsonResult WarningMessage(string msg)
        {
            var result = new Dictionary<String, Object> { { "success", false }, { "msg", msg } };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #region ConVertIlistToDataset
        public static DataSet ToDataSet<T>(IList<T> list) where T : class
        {
            Type elementType = typeof(T);
            DataSet ds = new DataSet();
            DataTable t = new DataTable();

            //add a column to table for each public property on T
            foreach (var propInfo in elementType.GetProperties())
            {
                Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;
                t.Columns.Add(propInfo.Name, ColType);
            }
            //go through each property on T and add each value to the table

            foreach (T item in list)
            {
                if (item != null)
                {
                    DataRow row = t.NewRow();
                    foreach (var propInfo in elementType.GetProperties())
                    {
                        row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
                    }
                    t.Rows.Add(row);
                }
            }
            ds.Tables.Add(t);
            return ds;
        }
        #endregion

        protected object GetDataFromForm(object obj, FormCollection values)
        {
            object val = null;
            foreach (var f in obj.GetType().GetProperties())
            {
                if (String.IsNullOrEmpty(values[f.Name])) continue;
                var temp = String.IsNullOrEmpty(values[f.Name + "_Value"]) ? values[f.Name] : values[f.Name + "_Value"];
                if (f.PropertyType.Name == System.Data.DbType.Guid.ToString())
                {
                    //if(String.IsNullOrEmpty(f.GetValue(obj,null).ToString())){

                    //}
                    val = f.GetValue(obj, null);
                }
                else

                    if (f.PropertyType.FullName.Contains(System.Data.DbType.Int32.ToString()))
                    {
                        val = Int32.Parse(temp);
                    }
                    else

                        if (f.PropertyType.FullName.Contains(System.Data.DbType.String.ToString()))
                        {
                            val = temp;
                        }
                        else

                            if (f.PropertyType.FullName.Contains(System.Data.DbType.DateTime.ToString()))
                            {
                                val = DateTime.ParseExact(temp, "dd/MM/yyyy", new CultureInfo("vi-VN"));
                            }
                            else

                                if (f.PropertyType.FullName.Contains(System.Data.DbType.Boolean.ToString()))
                                {
                                    val = temp.ToUpper() == "TRUE" ? true : false;
                                }
                f.SetValue(obj, val, null);
            }
            return obj;
        }
    }
}
